<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Goods\EditRequest;
use App\Service\Store\GoodsService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 *
 * 后台商品管理
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 陈奕文
 * @package App\Controller\store
 */
class GoodsController extends Controller
{

    /**
     * 商品服务
     * @Inject()
     * @var GoodsService
     */
    private $goods_service;

    /**
     * 商品搜索
     */
    public function search()
    {
        $params = $this->request->all();
        $goods_list = $this->goods_service->getGoodsList($params);
        $this->success($goods_list);
    }

    /**
     * 单个商品上架
     */
    public function ground()
    {
        $id = (int)$this->request->input('id', 0); // 商品ID
        0 === $id && $this->error();

        $result = $this->goods_service->dao->update($id, [
           'disable' => 1
        ]);
        !$result && $this->error("上架失败");
        $this->success($result);
    }

    /**
     * 单个商品删除
     */
    public function del()
    {
        $id = (int)$this->request->input('id', 0); // 商品ID
        0 === $id && $this->error();
        $exist = $this->goods_service->dao->checkExistById($id); // 检测不存在
        !$exist && $this->error("GOODS.NOT_FOUND");

        $result = $this->goods_service->dao->delete($id);
        !$result && $this->error();
        $this->success($result);
    }

    /**
     * 获取单个商品详细信息
     */
    public function info()
    {
        $id = (int)$this->request->input('id', 0); // 商品ID
        0 === $id && $this->error();

        $goods_info = $this->goods_service->getGoodsInfoById($id);
        $this->success($goods_info);
    }

    /**
     * 商品编辑
     * @param EditRequest $request
     */
    public function edit(EditRequest $request)
    {
        $params = $request->all();
        $this->goods_service->editData($params);
        $this->success();
    }

    /**
     * 创建商品
     * @param EditRequest $request
     */
    public function create(EditRequest $request)
    {
        $params = $request->all();
        $this->goods_service->createGoodsAndContentData($params);
        $this->success();
    }

    /**
     * 获得当前商品的sku
     */
    public function goodsSku()
    {
        $id = (int)$this->request->input('id', 0);
        0 === $id && $this->error();

        // 分页
        $page_size = 10;
        $skus = $this->goods_service->goods_sku_dao->getSkuByMap([
            'goods_id' => $id,
        ], 'id desc', null, $page_size)->toArray();

        $this->success($skus);
    }

    /**
     *
     * 获得单个商品的所有属性
     */
    public function properties()
    {
        $id = (int)$this->request->input('id', 0);
        0 === $id && $this->error();

        // 分页
        $page_size = 10;
        $properties = $this->goods_service->goods_property_dao->getPpyById($id, $page_size);

        $this->success($properties);
    }

    /**
     * 添加某个商品的商品属性
     */
    public function ppyAdd()
    {
        $id = (int)$this->request->input('id', 0);
        0 === $id && $this->error();

        $title = $this->request->input('title', ''); // 属性名
        empty($title) && $this->error('GOODS.PROPERTY_NOT_NULL');

        $sort = (int)$this->request->input('sort', 0); // 排序

        $result = $this->goods_service->goods_property_dao->create([
            'goods_id' => $id,
            'title' => $title,
            'sort' => $sort
        ]);
        !$result && $this->error();

        $this->success($result);
    }


    /**
     * 获得单个商品的单个属性的所有属性值
     */
    public function ppyValues()
    {
        $id = (int)$this->request->input('property_id', 0);
        0 === $id && $this->error();
        // 分页
        $page_size = 10;
        $values = $this->goods_service->goods_ppy_values_dao->getValuesByPpyId($id, $page_size);
        $this->success($values);
    }


    /**
     * 添加单个商品单个属性的属性值
     */
    public function ppyValueAdd()
    {
        $id = (int)$this->request->input('property_id', 0);
        0 === $id && $this->error();

        $value = $this->request->input('value', ''); // 属性值
        empty($value) && $this->error('GOODS.PPY_VAL_NOT_NULL');

        $this->error();
        $result = $this->goods_service->goods_ppy_values_dao->create([
            'property_id' => $id,
            'value' => $value,
        ]);
        !$result && $this->error();

        $this->success($result);
    }

    /**
     * 删除多条属性
     */
    public function ppyDel()
    {
        $ids = $this->request->input('property_ids',[]);
        if (!is_array($ids) || count($ids) <= 0) {
            $this->error("不是数组或者为空数组");
        }
        foreach ($ids as $id) {
            !is_numeric($id) && $this->error("存在非整数");
        }
        $exits =$this->goods_service->goods_property_dao->checkExistByIds($ids);
        !$exits && $this->error("不存在属性");

        $result = $this->goods_service->goods_property_dao->deleteByIds($ids);
        !$result && $this->error();

        $this->success($result);
    }

    /**
     * 删除多个属性值
     */
    public function ppyValueDel()
    {
        $ids = $this->request->input('property_value_ids',[]);
        if (!is_array($ids) || count($ids) <= 0) {
            $this->error("不是数组或者为空数组");
        }
        foreach ($ids as $id) {
            !is_numeric($id) && $this->error("存在非整数");
        }
        $exists = $this->goods_service->goods_ppy_values_dao->checkExistByIds($ids);
        !$exists && $this->error("属性值不存在");

        $result = $this->goods_service->goods_ppy_values_dao->deleteByIds($ids);
        !$result && $this->error();

        $this->success($result);

    }

    /**
     * 获得所有顶级分类
     */
    public function topCates()
    {
       $categories = $this->goods_service->goods_category_dao->getTopCategory();
       $this->success($categories);
    }

    /**
     * 商品分类搜索
     */
    public function cateSearch()
    {
        $map = [];
        $pid = $this->request->input('pid'); // 上级分类ID
        !empty($pid) && $map['pid'] = $pid;

        $name = $this->request->input('name'); // 分类名称
        !empty($name) && $map['name'] = ['like', "%{$name}%"];

        $sort = 'sort desc,id desc';
        $select = ['id', 'created_at','pid', 'name', 'icon', 'sort'];
        $page_size = 20;
        $cate_list = $this->goods_service->goods_category_dao->getGoodsCateByMap($map, $sort, $select, $page_size)->toArray();

        // 拼接上级分类
        $cate_list['data'] = $this->goods_service->joinSuperCateData($cate_list['data']);

        $this->success($cate_list);
    }

    /**
     * 创建商品分类
     */
    public function cateAdd()
    {
        $super_cate_id = (int)$this->request->input('super_cate_id', 0); // 父级分类
        if ($super_cate_id !== 0) {
            $exists = $this->goods_service->goods_category_dao->checkCateExistById($super_cate_id);
            !$exists && $this->error('GOODS_CATEGORY.NOT_FOUND');
        }
        $data['pid'] = $super_cate_id;

        $name = $this->request->input('name'); // 分类名称
        empty($name) && $this->error('GOODS_CATEGORY.CATE_NAME_NOT_NULL');
        $data['name'] = $name;

        $icon = $this->request->input('icon'); // 图标
        empty($icon) && $this->error('GOODS_CATEGORY.ICON_NOT_NULL');
        $data['icon'] = $icon;

        $sort = (int)$this->request->input('sort', 0); // 排序
        $data['sort'] = $sort;

        $result = $this->goods_service->goods_category_dao->create($data);
        !$result && $this->error('GOODS_CATEGORY.CREATE_FAIL');

        $this->success();
    }

    /**
     * 编辑商品分类
     */
    public function cateEdit()
    {
        $cate_id = (int)$this->request->input('cate_id', 0); // 当前分类ID
        !$cate_id && $this->error('GOODS_CATEGORY.ID_NOT_NULL');

        $name = $this->request->input('name'); // 分类名称
        empty($name) && $this->error('GOODS_CATEGORY.CATE_NAME_NOT_NULL');
        $data['name'] = $name;

        $icon = $this->request->input('icon'); // 图标
        empty($icon) && $this->error('GOODS_CATEGORY.ICON_NOT_NULL');
        $data['icon'] = $icon;

        $sort = (int)$this->request->input('sort', 0); // 排序
        $data['sort'] = $sort;

        $result = $this->goods_service->goods_category_dao->update($cate_id, $data);
        !$result && $this->error('GOODS_CATEGORY.UPDATE_FAIL');

        $this->success();
    }

    /**
     * 删除商品分类
     */
    public function cateDel()
    {
        $id = (int)$this->request->input('cate_id',0);

        $exists = $this->goods_service->goods_category_dao->checkCateExistById($id);
        !$exists && $this->error('GOODS_CATEGORY.NOT_FOUND');

        $result = $this->goods_service->goods_category_dao->delete($id);
        !$result && $this->error('删除失败');

        $this->success();
    }
    

}
